package com.algorithm.ch1.yjx.array;

import java.util.Arrays;

/**
 * @author yuanjianxin
 * @date 2018/8/13 20:21
 */
public class PlusOne {

    public static void main(String[] args) {
        int[] arr = {2, 4, 9, 3, 9};
        int[] ret = plusOne(arr);
        System.out.println(Arrays.toString(ret));
    }

    public static int[] plusOne(int[] digits) {
        add(digits, digits.length - 1);
        if (digits[0] == 0) {
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            return result;
        }
        return digits;
    }

    public static int add(int[] digits, int index) {
        for (int i = index; i >= 0; i--) {
            if (digits[i] == 9) {
                digits[i] = 0;
                int result = add(digits, --i);
                i = result;
            } else {
                digits[i] += 1;
                return -1;
            }
        }
        return -1;
    }
}
